package ua.kpi_java_training.tyshchenko.module1;

import java.util.Scanner;

/**
 * Class get string from the console.
 * The console output all palindromes which are in input string.
 *
 * @version 1.2.1 26 May 2013
 * @author Vlad Tyshchenko
 */
public class Palindrom {
    String line = "";
    String palindroms = "";

    public static void main(String[] args) {
        Palindrom pal = new Palindrom();
        pal.inputLine();
        pal.checkLine();
        pal.enumerationLine();
        pal.output();
    }

    /**
     * Method gets input data.
     */
    void inputLine() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter string:");
        line = sc.nextLine();
    }

    /**
     * Method checks the correctness of input line.
     */
    void checkLine() {
        if (line.length() <= 2) {
            System.out.println("Error");
            System.exit(0);
        }
    }

    /**
     * Method checks palindrom which has one central symbol (exam. eRe) in line.
     *
     * @param centerIndex - central index of palindrom.
     * @param lastIndex - lateral index of palindrom.
     * @return test - line that contains a palindrome.
     */
    String getPalindromOneCenterSymbol(int centerIndex, int lastIndex) {
        String test = null;
        if (line.codePointAt(centerIndex - lastIndex) == line.codePointAt(centerIndex + lastIndex)) {
            test = line.substring(centerIndex - lastIndex, centerIndex + lastIndex + 1);
        }
        return test;
    }

    /**
     * Method checks palindrom which has two central symbol (exam. eRRe) in line.
     *
     * @param centerIndex - central index of palindrom.
     * @param lastIndex - index of the left side of the palindrom.
     * @return test - line that contains a palindrome.
     */
    String getPalindromTwoCenterSymbol(int centerIndex, int lastIndex) {
        final int CONSTANT_INDEX = 2; // this constant use to get the right last index
        String test = null;
        boolean checkSide = (line.codePointAt(centerIndex - lastIndex) == line.codePointAt(centerIndex + lastIndex + 1));
        boolean checkCenter = line.codePointAt(centerIndex) == line.codePointAt(centerIndex + 1);
        if (checkSide && checkCenter) {
            test = line.substring(centerIndex - lastIndex, centerIndex + lastIndex + CONSTANT_INDEX);
        }
        return test;
    }

    /**
     * Method finds all palindroms which has one central index.
     *
     * @param centerIndex - central index of palindrom.
     */
    void searchPalindrom(int centerIndex) {
        int size = line.length();
        String result = null;
        for (int lastIndex = 1; ((lastIndex <= centerIndex) && (lastIndex <= (size - centerIndex - 1))); lastIndex++) {
            result = getPalindromOneCenterSymbol(centerIndex, lastIndex);
            if (result == null) {
                break;
            }
            palindroms += (result + "\n");
        }
        result = null;
        for (int lastIndex = 1; ((lastIndex <= centerIndex) && (lastIndex <= (size - centerIndex - 2))); lastIndex++) {
            result = getPalindromTwoCenterSymbol(centerIndex, lastIndex);
            if (result == null) {
                break;
            }
            palindroms += (result + "\n");
        }
    }

    /**
     * Method iterates all central index of palindrom in line.
     */
    void enumerationLine() {
        int size = line.length();
        for (int centerIndex = 1; centerIndex < size - 1; centerIndex++) {
            searchPalindrom(centerIndex);
        }
    }

    /**
     * Print all palindroms which consist in line.
     */
    void output() {
        System.out.println("All palindroms: ");
        if (palindroms.equals("")) {
            System.out.println("Palindroms not found");
        }
        System.out.println(palindroms);
    }

}
